/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.SoapBinding;
import han.bifang.gwt.client.common.entity.Version;
import han.bifang.gwt.client.common.entity.WebServicesMethodWrapper;
import han.bifang.gwt.client.common.entity.WsdlWrapper;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CreateVersionConfigContent extends Content {

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private VerticalPanel panel = null;

	private VersionQosPanel vp;

	private ArrayList ml;

	private String flag;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			// s.setId((String)res.getResponse());
			Version v = (Version)context.get(ServiceConstants.VERSION);
			HashMap p = new HashMap();
			p.put("id", (String) res.getResponse());
			p.put("v", v.getVersion());
			
			context.remove(ServiceConstants.VERSION);
			context.remove(ServiceConstants.BINDING);
			
			listener.onContentChanged(
					ServiceContentConstants.CREATE_VERSION_CONFIG,
					ServiceContentConstants.VERSION_ROOT, p);
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public CreateVersionConfigContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context) {
		this.cons = cons;
		this.context = context;
		this.service = service;
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");

		Object b = context.get(ServiceConstants.BINDING);
		if (b instanceof WsdlWrapper) {
			flag = "webservices";
		} else if (b instanceof JmsBindingWrapper) {
			flag = "jms";
		} else if (b instanceof HttpBinding) {
			flag = "http";
		}else if (b instanceof SoapBinding) {
			flag = "soap";
		}

		initWidget(panel);

	}

	public void onLoad() {
		HTML h = new HTML(cons.configVersion());
		panel.add(h);

		vp = new VersionQosPanel(cons,context, flag);
		panel.add(vp);

		if (flag.equals("webservices")) {
			WsdlWrapper w = (WsdlWrapper) context.get(ServiceConstants.BINDING);
			ArrayList l = w.getMethods();
			FlexTable table = new FlexTable();
			table.setWidth("100%");
			table.setCellPadding(1);
			table.setCellSpacing(1);
			table.setBorderWidth(1);

			ColumnFormatter cf = table.getColumnFormatter();
			cf.setWidth(0, "20%");
			cf.setWidth(1, "80%");

			CellFormatter ff = table.getCellFormatter();
			ff.setStyleName(0, 0, "tablecol");
			ff.setStyleName(0, 1, "tablecol");

			table.setHTML(0, 0, cons.methodName());
			table.setHTML(0, 1, cons.methodDetail());
			ml = new ArrayList();
			for (int i = 0, j = l.size(); i < j; i++) {
				WebServicesMethodWrapper wm = (WebServicesMethodWrapper) l
						.get(i);
				table.setHTML(i + 1, 0, wm.getMethod().getMethodName());
				MethodQosPanel mm = new MethodQosPanel(cons,null);
				table.setWidget(i + 1, 1, mm);
				ml.add(mm);
			}

			panel.add(table);

		}

		HorizontalPanel hp = new HorizontalPanel();

		Button p = new Button(cons.previous());
		p.addClickListener(new ClickListener() {
			public void onClick(Widget send) {

				listener.onContentChanged(
						ServiceContentConstants.CREATE_VERSION_CONFIG,
						ServiceContentConstants.CREATE_VERSION_COMM, null);

			}
		});

		Button b = new Button(cons.create());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget send) {

				Version v = (Version) context.get(ServiceConstants.VERSION);
				Object o = context.get(ServiceConstants.BINDING);
				ArrayList versionqos = vp.getVersionQos();
				if(versionqos == null)
					return;
				if (flag.equals("webservices")) {
					WsdlWrapper w = (WsdlWrapper) context
							.get(ServiceConstants.BINDING);
					ArrayList l = w.getMethods();
					for (int i = 0, j = l.size(); i < j; i++) {
						WebServicesMethodWrapper wm = (WebServicesMethodWrapper) l
								.get(i);
						MethodQosPanel p = (MethodQosPanel) ml.get(i);
						if(p.getMethodQos() == null)
							return;
						wm.setQos(p.getMethodQos());
					}
				}

				ArrayList r = new ArrayList();
				r.add(v);
				r.add(o);
				r.add(versionqos);

				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.service.CreateVersionAction");
				req.setRequest(r);

				service.call(req, cb);

			}
		});
		hp.add(p);
		hp.add(new HTML("&nbsp;"));
		hp.add(b);

		panel.add(hp);

		// panel.add(b);

	}

}
